package com.zut.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zut.subject.application.convert.convertMapping;
import com.zut.subject.application.convert.convertSubjectLabel;
import com.zut.subject.application.dto.SubjectLabelDto;
import com.zut.subject.application.dto.SubjectMappingDto;
import com.zut.subject.common.entity.Result;
import com.zut.subject.domain.entity.SubjectLabelBo;
import com.zut.subject.domain.entity.SubjectMappingBo;
import com.zut.subject.domain.service.SubjectLabelDomainService;
import com.zut.subject.infra.basic.entity.SubjectLabel;
import com.zut.subject.infra.basic.entity.SubjectMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {


    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;
    /**
     * 添加标签分类
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/add")
    private Result<Boolean> add(@RequestBody SubjectLabelDto subjectLabelDto){
             Result result =new Result();
        try {

            if(log.isInfoEnabled()){
                log.info("subjectLabelDomainService.add.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            //引入依赖guava，使用Google包下的Preconditions校验传入参数
            Preconditions.checkNotNull(subjectLabelDto.getLabelName(),"前端传入标签名字为空");
            Preconditions.checkNotNull((subjectLabelDto.getSortNum()),"您传入的标签排行数字为空");
            //将dto类型转化为bo类型并传到domain层
            boolean add = subjectLabelDomainService.add(convertSubjectLabel.INSTANCE.toSubjectLabelBo(subjectLabelDto));
            //成功返回true
            return result.success(add);
        } catch (Exception e) {
            //日志提示参数校验错误
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return result.fail(e.getMessage());
        }
    }

    /**
     * 修改标签分类
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/update")
    private Result<String> update(@RequestBody SubjectLabelDto subjectLabelDto){
        Result result =new Result();

        try {
            //判断传入id不能为空
            Preconditions.checkNotNull(subjectLabelDto.getId(),"您传入的id为空");
            //将dto转化为bo传入domain层
            SubjectLabelBo subjectLabelBo = convertSubjectLabel.INSTANCE.toSubjectLabelBo(subjectLabelDto);

            //输出日志
            if (log.isInfoEnabled()){
                log.info("controller向domain传入数据为:{}",subjectLabelBo);
            }
            boolean update = subjectLabelDomainService.update(subjectLabelBo);

            //根据update值判断是否修改成功，成功返回true，失败返回提示信息
            if (update==true)
                return result.success(update);
            else return result.fail("您输入的id有误！");

        } catch (Exception exception) {
            //失败返回失败信息
            return result.fail(exception.getMessage());
        }


    }

    /**
     * 删除标签
     */
    @PostMapping("/delete")
    private Result<String> delete(@RequestBody SubjectLabelDto subjectLabelDto){
        Result result =new Result();

        try {
            //判断传入id不能为空
            Preconditions.checkNotNull(subjectLabelDto.getId(),"您传入的id为空");
            //将dto转化为bo传入domain层
            SubjectLabelBo subjectLabelBo = convertSubjectLabel.INSTANCE.toSubjectLabelBo(subjectLabelDto);

            //输出日志
            if (log.isInfoEnabled()){
                log.info("controller向domain传入数据为:{}",subjectLabelBo);
            }
            boolean update = subjectLabelDomainService.delete(subjectLabelBo);

            //根据update值判断是否修改成功，成功返回true，失败返回提示信息
            if (update==true)
                return result.success(update);
            else return result.fail("您输入的id有误！");

        } catch (Exception exception) {
            //失败返回失败信息
            return result.fail(exception.getMessage());
        }


    }

    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDto>> queryLabelByCategory(@RequestBody SubjectMappingDto subjectMappingDto){
        Result result =new Result();

        try {
            //判断传入参数是否为空
            Preconditions.checkNotNull(subjectMappingDto.getCategoryId(),"您传入的分类Id为空");
            //输出日志
            if (log.isInfoEnabled()){
                log.info("controller向domain传入数据为:{}",subjectMappingDto);
            }
            SubjectMappingBo subjectMappingBo = convertMapping.INSTANCE.toSubjectMappingBo(subjectMappingDto);
            List<SubjectLabelBo> boList= subjectLabelDomainService.queryLabelByCategory(subjectMappingBo);
            //将传出的boList集合转化为DtoList集合
            List<SubjectLabelDto> DtoList = convertSubjectLabel.INSTANCE.toSubjectLabel(boList);
            if (log.isInfoEnabled()){
                log.info("controller向外传出数据为:{}",DtoList);
            }
            return result.success(DtoList);

        } catch (Exception e) {
            return result.fail(e.getMessage());
        }

    }




}
